package load;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.uestc.vshop.ytgoodprintandroid.R;
import com.uestc.vshop.ytgoodprintandroid.base.MainActivity;
import com.uestc.vshop.ytgoodprintandroid.base.YTBaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/3.
 */

public class LoginFragment extends YTBaseFragment {

    public static LoginFragment newInstance() {
        return new LoginFragment();
    }
    private OnLoginSuccessListener mOnLoginSuccessListener;

    @BindView(R.id.toolbar) Toolbar toolbar;
    @BindView(R.id.et_account) EditText mEtAccount;
    @BindView(R.id.et_password) EditText mEtPassword;
    @BindView(R.id.btn_login) Button mBtnLogin;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);


        ButterKnife.bind(this, view);
        initView(view);

        return view;

    }
    private void initView(View view) {
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);


        toolbar.setTitle(R.string.login);

        initToolbarNav(toolbar);
        toolbar.setNavigationIcon(null);

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strAccount = mEtAccount.getText().toString();
                String strPassword = mEtPassword.getText().toString();
                if (TextUtils.isEmpty(strAccount.trim())) {
                    Toast.makeText(_mActivity, R.string.error_username, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(strPassword.trim())) {
                    Toast.makeText(_mActivity, R.string.error_pwd, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (strAccount == "admin" && strPassword == "123456"){
                    // 登录成功
                    mOnLoginSuccessListener.onLoginSuccess(strAccount);
                    startActivity(new Intent(getActivity(),MainActivity.class));

                }



            }
        });


    }

    public interface OnLoginSuccessListener {
        void onLoginSuccess(String account);
    }
}
